/*
 * Tint Browser for Android
 * 
 * Copyright (C) 2012 - to infinity and beyond J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.cnbrowser.android.ui.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.viewpagerindicator.LinePageIndicator;

import com.cnbrowser.android.R;
import com.cnbrowser.android.ui.cbrowser.HomepageFragmentAdapter;
import com.cnbrowser.android.ui.cbrowser.StartPageItemClickedListener;
import com.cnbrowser.android.ui.fragments.homepage.LeftFragment;
import com.cnbrowser.android.ui.fragments.homepage.MiddleFragment;
import com.cnbrowser.android.ui.fragments.homepage.RightFragment;

import java.util.ArrayList;
import java.util.List;

public class ChinaBrowserStartPageFragment extends StartPageFragment {

    private final MiddleFragment middleFragment;
    private final LeftFragment leftFragment;
    private final RightFragment rightFragment;
    private List<Fragment> mFragments;

    public ChinaBrowserStartPageFragment() {
        mFragments = new ArrayList<Fragment>(3);
        leftFragment = new LeftFragment();
        mFragments.add(leftFragment);
        middleFragment = new MiddleFragment();
        mFragments.add(middleFragment);
        rightFragment = new RightFragment();
        mFragments.add(rightFragment);
    }

    @Override
    public void setOnStartPageItemClickedListener(StartPageItemClickedListener listener) {
        super.setOnStartPageItemClickedListener(listener);
        middleFragment.setOnStartPageItemClickedListener(listener);
        leftFragment.setOnStartPageItemClickedListener(listener);
        rightFragment.setStartPageItemClickedListener(listener);
    }

    @Override
    protected int getStartPageFragmentLayout() {
        return R.layout.china_browser_start_page_fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        if (view != null) {
            ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
            Activity activity = getActivity();
            if (activity != null) {

                FragmentManager supportFragmentManager = ((FragmentActivity) activity).getSupportFragmentManager();
                PagerAdapter adapter = new HomepageFragmentAdapter(supportFragmentManager, mFragments);
                pager.setAdapter(adapter);
            }

            LinePageIndicator indicator = (LinePageIndicator) view.findViewById(R.id.indicator);
            indicator.setViewPager(pager);
            indicator.setCurrentItem(1);
        }

        return view;
    }
}
